/*  

Cafechess Library: Cover basic chess programming needs, like GUI, Engine integration, 
FEN and PGN parsing.

                 \\|//
                 (o o)
--------------ooO-(_)-Ooo----------------------------------
Copyright (C) 2009  By Gregory A. Prentice Snoqualmie, WA
                    www.cafechess.org
                    cafechess@comcast.net
                      Oooo
--------------oooO----(  )---------------------------------
              (  )    (_/
               \_)

Microsoft Public License (Ms-PL)
 
Revisions:

Author        Date    Description:

*/

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace Cafechess.Forms
{
  public class UpdateTextThreaded : UpdateThreaded
  {
    /// <summary>
    /// The new string to add/overwrite in the control
    /// </summary>
    string coNewText;
    /// <summary>
    /// Flag that signals if we want to append or overwrite existing text in the control.
    /// </summary>
    bool coClearText;
    /// <summary>
    /// Public constructor
    /// </summary>
    /// <param name="control"></param>
    public UpdateTextThreaded(System.Windows.Forms.TextBox control): base(control)
    {
      coNewText = null;
    }

    /// <summary>
    /// Append text to the current textbox control.
    /// </summary>
    /// <param name="str"></param>
    public void Append(string str)
    {
      Append(str, false);
    }
    /// <summary>
    /// Overwrite the text in the current control.
    /// </summary>
    /// <param name="str"></param>
    public void Overwrite(string str)
    {
      Append(str, true);
    }
    /// <summary>
    /// Call when you want the UI text box to be updated with the values.
    /// </summary>
    private void Append(string str, bool clear)
    {
      if (str != null)
      {
        coNewText = str;
        coClearText = clear;
        Update();
      }
    }
    public string Text
    {
      set { Append(value, true); }
    }

    protected override void ResetUI()
    {
      lock (coControl)
      {
        TextBox control = coControl as TextBox;
        control.Text = "";
      }
    }

     /// <summary>
    /// This is the UIEventHandler delegate that get's called to actually
    /// update the user interface.
    /// </summary>
    protected override void UpdateUI()
    {
      lock (coControl)
      {
        TextBox control = coControl as TextBox;
        if (coClearText)
          control.Text = coNewText;
        else
          control.AppendText(coNewText);
        coClearText = false;
      }
    }

  }
}
